package com.example.demo.server.common;

import com.example.demo.ds.MyList;
import com.example.demo.server.controller.ArticleController;
import com.example.demo.server.model.ArticleInfo;

import java.util.List;


public class ArticleUtils {
    //标题截取长度
    private static final int _TITLE_LENGTH = 17;
    //摘要截取长度
    private static final int _SUMMARY_LENGTH = 32;
    //正文换行截取长度
    private static final int _CONTENT_LENGTH = 49;

    public static void substringList(MyList<ArticleInfo> myList) {
        List<ArticleInfo> list = myList.upgrade();
        if(list != null && list.size() != 0) {
            // 并发的方式处理 list 集合
            list.stream().parallel().forEach((art) -> {
                //标题截取
                if(art.getTitle().length() > _TITLE_LENGTH) {
                    art.setTitle(art.getTitle().substring(0, _TITLE_LENGTH) + "...");
                }
                //摘要截取
                if(art.getSummary().length() > _SUMMARY_LENGTH) {
                    art.setSummary(art.getSummary().substring(0, _SUMMARY_LENGTH) + "...");
                }
            });
        }
    }

    public static void printBriefBlog(ArticleInfo articleInfo, int index) {
        System.out.println("|                                                                                |");
        System.out.printf("|     +———[%03d]———————————————————————————————————————————————————————————+      |\n", index);
        System.out.printf("|     |     [%-20s                                         |      |\n", articleInfo.getTitle());
        System.out.printf("|     |   %s  read: %-4d                                 |      |\n", articleInfo.getCreatetime(), articleInfo.getRcount());
        System.out.printf("|     | %-35s                               |      |\n", articleInfo.getSummary());
        System.out.println("|     |               [detail]       [update]       [delete]              |      |");
        System.out.println("|     +———————————————————————————————————————————————————————————————————+      |");
        System.out.println("|                                                                                |");
    }

    public static void printBriefLowBlog(ArticleInfo articleInfo, int index) {
        System.out.println("|                                                                                |");
        System.out.printf("|     +———[%03d]———————————————————————————————————————————————————————————+      |\n", index);
        System.out.printf("|     |     [%-20s                                         |      |\n", articleInfo.getTitle());
        System.out.printf("|     |   %s  read: %-4d                                 |      |\n", articleInfo.getCreatetime(), articleInfo.getRcount());
        System.out.printf("|     | %-35s                               |      |\n", articleInfo.getSummary());
        System.out.println("|     |                             [detail]                              |      |");
        System.out.println("|     +———————————————————————————————————————————————————————————————————+      |");
        System.out.println("|                                                                                |");
    }


    public static void setTittle(ArticleInfo articleInfo) {
        String title = APPUtils.titleInput();
        articleInfo.setTitle(title);
    }
    public static void setSummary(ArticleInfo articleInfo) {
        String summary = APPUtils.summaryInput();
        articleInfo.setSummary(summary);
    }
    public static void setContent(ArticleInfo articleInfo) {
        String content = APPUtils.contentInput();
        articleInfo.setContent(content);
    }
    public static boolean submit(ArticleInfo articleInfo) {
        if(!APPUtils.isAllHasLength(articleInfo.getTitle(), articleInfo.getSummary(), articleInfo.getContent())) {
            APPUtils.printPopUp("ERROR: Please input title, summary and content!");
            return false;
        }else {
            ArticleController articleController = new ArticleController();
            articleController.publish(articleInfo);
            return true;
        }
    }

    public static boolean update(ArticleInfo articleInfo) {
        if(!APPUtils.isAllHasLength(articleInfo.getTitle(), articleInfo.getSummary(), articleInfo.getContent())) {
            APPUtils.printPopUp("ERROR: Please input title, summary and content!");
            return false;
        }else {
            ArticleController articleController = new ArticleController();
            articleController.update(articleInfo);
            return true;
        }
    }



    public static void interceptContent(String content) {
        int i = 0;
        while(i + _CONTENT_LENGTH <= content.length()) {
            //防止两个回车符在一起生效
            if(content.charAt(i + _CONTENT_LENGTH - 1) == '\n') {
                System.out.print(content.substring(i, i + _CONTENT_LENGTH));
            }else {
                if(i + _CONTENT_LENGTH < content.length() && content.charAt(i + _CONTENT_LENGTH) == '\n') {
                    System.out.print(content.substring(i, i + _CONTENT_LENGTH));
                }else {
                    System.out.println(content.substring(i, i + _CONTENT_LENGTH));
                }
            }
            i += _CONTENT_LENGTH;
        }
        System.out.println(content.substring(i));
    }

    public static void printArticle(ArticleInfo articleInfo) {
        System.out.printf("                           %s\n", articleInfo.getTitle());
        System.out.printf("                  %s  read: %-4d\n", articleInfo.getCreatetime(), articleInfo.getRcount());
        System.out.println();
        interceptContent(articleInfo.getContent());
    }

}

